package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.model.*;
import com.example.demo.service.*;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@org.springframework.web.bind.annotation.RestController
public class RestController {

    @Resource
    private ScheduleService scheduleService;
    @Resource
    private ParentService parentService;
    @Resource
    private StudentService studentService;
    @Resource
    private LinkService linkService;
    @Resource
    private StuInformationService stuInformationService;
    @Resource
    private GradesService gradesService;
    @Resource
    private AnnouncementService announcementService;

    //学生位置的集合
    private Map<String, Position> student =new HashMap<String, Position>();

    //获取学生课表
    @RequestMapping("/schedule/{phone}")
    public List<String> getSchedule(@PathVariable("phone") String phone){
        List<String> stuPhone = linkService.findStudent(phone);
        List<String > schedule = new ArrayList<>();
        for (String str:stuPhone){
            try {
                schedule.add(scheduleService.findSchedule(stuInformationService.findClazz(str)).toString());
            } catch (Exception e) {
            }
        }
        return schedule;
    }

    //学生登录
    @RequestMapping("/stuLogin/{Phone}/{Password}")
    public String studentLogin(@PathVariable("Phone") String Phone,@PathVariable("Password") String Password){
        if (studentService.exist(Phone,Password)) {
            Position position=new Position(Phone,0.0,0.0);
            if(student.get(Phone)!=null){
                return "登录成功";
            }
            student.put(Phone+"",position);
            return "登录成功";
        }
        else
            return null;
    }

    //家长登录
    @RequestMapping("/parentLogin/{Phone}/{Password}")
    public String parentLogin(@PathVariable("Phone") String Phone,@PathVariable("Password") String Password){
        if (parentService.exist(Phone,Password))
            return "success";
        else
            return "failure";
    }

    //接收学生发送的定位，保存到student集合中
    @RequestMapping("/student/{phone}/{Latitude}/{Longitude}")
    public void setPosition(@PathVariable("phone") String phone,@PathVariable("Latitude") double Latitude,@PathVariable("Longitude") double Longitude){
        Position position=student.get(phone);
        if (position!=null){
            position.setLatitude(Latitude);
            position.setLongitude(Longitude);
        }
    }

    //发送学生定位,Phone为家长手机号
    @RequestMapping("/position/{phone}")
    public List<String> getPosition(@PathVariable("phone") String phone){
        List<String> stuPhone = linkService.findStudent(phone);
        List<String > positions = new ArrayList<>();
        for (String str:stuPhone){
            if (student.get(str)!=null)
                positions.add(student.get(str).toString());
        }
        if (positions!=null){
            return positions;
        }
        return null;
    }

    //SOS请求
    @RequestMapping("/setSOS/{phone}")
    public void setSOS(@PathVariable("phone") String phone){
        Position position=student.get(phone);
        if (position!=null){
            position.setSOS(true);
        }
    }

    //发送SOS信号
    @RequestMapping("/getSOS/{phone}")
    public String getSOS(@PathVariable("phone") String phone){
        List<String> stuPhone= new ArrayList<>();
        List<Position> positions=new ArrayList<>();
        try {
            stuPhone = linkService.findStudent(phone);
        } catch (Exception e) {
        }
        for (String str:stuPhone){
            positions.add(student.get(str));
        }
        System.out.println(stuPhone);
        System.out.println(positions);
        for (Position position:positions){
            if (position!=null&&position.getSOS()==true){
                System.out.println(position.getSOS());
                return position.getPhone();
            }
        }
        return null;
    }

    //关闭SOS信号
    @RequestMapping("/closeSOS/{phone}")
    public void closeSOS(@PathVariable("phone") String phone){
         student.get(phone).setSOS(false);
    }

    //发送学生成绩，phone为家长手机号
    @RequestMapping("/getGrades/{phone}")
    public List<String> getGrades(@PathVariable("phone") String phone){
        List<String> stuPhone=linkService.findStudent(phone);
        List<String> allGrades = new ArrayList<>();
        List<Grades> grades = new ArrayList<>();
        for (String str:stuPhone){
            try {
                grades=gradesService.findGrades(str);
                for (Grades gra:grades){
                    gra.setStuPhone(str);
                    allGrades.add(gra.toString());
                }
            } catch (Exception e) {
            }
        }
        return allGrades;
    }

    //发送公告，phone为家长手机号
    @RequestMapping("/getAnnouncement/{phone}")
    public List<String> getAnnouncement(@PathVariable("phone") String phone){
        Set<String> clazz=new HashSet<>();
        List<String> announcements=new ArrayList<>();
        List<String> stuPhone=new ArrayList<>();
        try {
            stuPhone=linkService.findStudent(phone);
        }catch (Exception e){

        }
        for (String str:stuPhone){
            try {
                clazz.add(stuInformationService.findClazz(str));
            }catch (Exception e){
            }
        }
        for(String str:clazz){
            List<Announcement> announcement=new ArrayList<>();
            try {
                announcement = announcementService.findAnnouncement(str);
            }catch (Exception e){
            }
            for (Announcement ann: announcement){
                announcements.add(ann.toString());
            }
        }
        return announcements;
    }

    //发送学生信息，phone为家长手机号
    @RequestMapping("/getStudent/{phone}")
    public List<String> getStudent(@PathVariable("phone") String phone){
        List<String> stuPhone= null;
        try {
            stuPhone = linkService.findStudent(phone);
        } catch (Exception e) {
        }
        List<String> stuInformations =new ArrayList<>();
        for (String str:stuPhone){
            try {
                stuInformations.add(stuInformationService.findStuInformation(str).toString());
            } catch (Exception e) {
            }
        }
        return stuInformations;
    }
}
